<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
	body, html,
		#allmap {width: 100%;height: 100%;overflow: hidden;margin:20;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=tDKISibLjGgNsN2i7WBdB4OW5UyRIDyD"></script>
	<script type="text/javascript" src="./qwebchannel.js"></script>
	<title>地图展示</title>
</head>
<body>
	<div id="allmap"></div>
	<textarea id="output"></textarea><br />
	<input id="input" />
	<input type="submit" id="send" value="Send" onclick="javascript:click();" />
</body>
</html>
<script type="text/javascript">
	function GpsPose(lat,lon,roll,pitch,yaw){
		var self = this;
		self.x = lat;
		self.y = lon;
		self.roll = roll;
		self.pitch = pitch;
		self.yaw = yaw;
	}
	var gpsPose = new GpsPose(0.0,0.0,0.0,0.0,0.0);
	var gspPoseLast = new GpsPose(0.0,0.0,0.0,0.0,0.0);
	
	
	var isGpsInit = false;

	// 百度地图API功能
	var map = new BMap.Map("allmap");    // 创建Map实例
	map.centerAndZoom(new BMap.Point(114.30, 30.60), 18);  // 初始化地图,设置中心点坐标和地图级别
	
	//添加地图类型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));	  
	map.setCurrentCity("武汉");          // 设置地图显示的城市 此项是必须设置的
	map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
	
	
	//添加地图鼠标移动事件
	map.addEventListener("mousemove", function (e) {
        //content.getCoordinate(e.point.lng, e.point.lat);
    });
	
	var myIcon = new  BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/Mario.png", new BMap.Size(32, 70), { 
		imageOffset: new BMap.Size(0, 0)                //图片的偏移量。为了是图片底部中心对准坐标点。
	});
	
	var carMk;
	function updateCarPosition(){
		carPosition = new BMap.Point(gpsPose.x, gpsPose.y);
		if(!isGpsInit)
		{
			carMk = new BMap.Marker(carPosition,{icon:myIcon});
			carMk.setPosition(carPosition);
			carMk.setRotation(gpsPose.yaw);
			
			map.addOverlay(carMk);
			map.setCenter(carPosition);
			
			isGpsInit = true;
			gspPoseLast.x  = gpsPose.x;
			gspPoseLast.y  = gpsPose.y;
			gspPoseLast.roll  = gpsPose.roll;
			gspPoseLast.pitch  = gpsPose.pitch;
			gspPoseLast.yaw  = gpsPose.yaw;
			
			return;
		}
		carMk.setPosition(carPosition);
		carMk.setRotation(gpsPose.yaw);
		map.setCenter(carPosition);
		
		
		//画行驶轨迹		
		var polyline = new BMap.Polyline([
			new BMap.Point(gspPoseLast.x, gspPoseLast.y),
			new BMap.Point(gpsPose.x, gpsPose.y)
			],
			{strokeColor:"red", strokeWeight:6, strokeOpacity:0.5}
		);
		map.addOverlay(polyline);
		
		gspPoseLast.x  = gpsPose.x;
		gspPoseLast.y  = gpsPose.y;
		gspPoseLast.roll  = gpsPose.roll;
		gspPoseLast.pitch  = gpsPose.pitch;
		gspPoseLast.yaw  = gpsPose.yaw;
		//alert("update gpsPoseLast: x"+ gspPoseLast.x + "  y: "+gspPoseLast.y);
	}
	
	
	//BEGIN SETUP
	function output(message)
	{
		var output = document.getElementById("output");
		output.innerHTML = output.innerHTML + message + "\n";
	}
	output("setting up QWebChannel.");
	
	//WebChannel setup
	var content;
	
	new QWebChannel(qt.webChannelTransport, function(channel) {
		// make dialog object accessible globally
		content = channel.objects.content;

		document.getElementById("send").onclick = function() {
			var input = document.getElementById("input");
			var text = input.value;
			if (!text) {
				return;
			}

			output("Sent message: " + text);
			input.value = "";
			content.receiveText(text);
		}

		content.sendText.connect(function(message) {
			output("Received message: " + message);
		});
		
		//更新汽车的位置
		content.updatePose.connect(function(x,y,roll,pitch,yaw){
			gpsPose.x = x;
			gpsPose.y = y;
			gpsPose.roll = roll;
			gpsPose.pitch = pitch;
			gpsPose.yaw = yaw;
		
			updateCarPosition();
		});
		
	
		content.receiveText("Client connected, ready to send/receive messages!");
		output("Connected to WebChannel, ready to send/receive messages!");
    });
</script>